import Vue from 'vue'
import { usePost} from "@/utils/request"
import { URL_link } from '@/constant/url.js'

export const state = () => ({
	mobileSessionInfo: {}, // 移动端session信息
	messageList: [],
	scrollMobileMessage: false, // 客户和代理人窗口滚动消息到底部
})

export const mutations = {
	setMobileSessionInfo(state, data) {
		state.mobileSessionInfo = data
	},
	setMessageList(state, data) {
		state.messageList = data
	},
	pushMessageList(state, data) {
		state.messageList.push(data)
	},
	// 设置用户和代理人窗口是否滚动消息到底部状态
	setScrollMobileMessage(state, data) {
		state.scrollMobileMessage = data
	},
}

export const actions = {
	postMsgMobile({ commit, state }, data) {
		usePost(URL_link.postIM, {
			query_tenant_id: LOCAL_CONFIG.query_tenant_id,
			session_uuid: state.mobileSessionInfo.session_id,
			client_id: state.mobileSessionInfo.client_id,
			client_name: state.mobileSessionInfo.client_name,
			adviser_id: LOCAL_CONFIG.agent_code,
			adviser_name: LOCAL_CONFIG.agent_name,
			msg_side: '0', // 1:发送普通消息给客户 0:发送普通消息给代理人
			aicc_msg_type: data.aicc_msg_type,
			aicc_msg_content: data.aicc_msg_content,
			// msg_send_time: nowTime
		}).then(res => {
			let item = {
				message_type:  '1', // message_type 1.用户->代理人 2.代理人->用户 3.代理人->大模型 4.大模型->代理人
				message_body_type: data.aicc_msg_type,
				create_time: res.data.create_time,
				message: data.aicc_msg_content
			}
			commit('pushMessageList', item)
			Vue.nextTick(() => {
				commit('setScrollMobileMessage', true)
			})
		})
	}
}